// pages/studyRecord/essay/essay.js
//获取应用实例
const app = getApp();
let tool = require('../../../utils/tool.js');
const httpUtils = require('../../../utils/httpUtils.js');
const bgMusic = wx.getBackgroundAudioManager()
var WxParse = require('../../../wxParse/wxParse.js');

//  正在录音的下标
let recordingIndex = 0;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
    essay: {},
    phrase: [],
    sentence: [],
    innerAudioContext: new Object(),
    is_show_translate: 0, //0不显示翻译  1显示
    is_class_pic: 0, //是否有课堂训练图片，默认0没有，1有
    essay_id: 1, //范文id
    isOpen: false, //播放开关
    starttime: "00:00", //正在播放时长
    duration: '00:00', // 总时长
    offset: 0, //记录播放的seek
    src: "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/Picture/2018-07-21/5b52df801b860.mp3", //音频的地址
    requiretext: "假定你是李华，正在一所英国学校学习暑期课程，遇到一些困难，希望得到学校辅导中心(Learning Center)的帮助。根据学校规定，你需要书面预约，请按下列的要求写一封信：1.本人简介 2.求助内容 3.约定时间 4.你的联系方式(Email: lihua@1236.com ; Phone:12345678)注意事项： 1.词数90词左右 2.文中不能出现真实的人名。", //题目要求
    flag: true, //提示弹窗的标志
    showVipModal: false, //开通vip弹窗组件是否显示
    isVip: false, //vip为	0 游客 1 VIP
    hasreview: 0, //0未复习，1已提交复习
    has_read: 1, //0无读一遍，1有读一遍
    hasRecord: 0, //0未录音，1有录音
    source: 0, //0是学习记录进入的，1是复习进入的
    task_id: '', //任务id
    sort: '14', //类别,写作读一遍
    voice: '',//自己录音的路径
    score: '-1',//语音识别的分数
    btnPlayStatus: [], //语音识别的三个按钮的状态
    can_submit: 1, //1 可以提交 0不可以提交
    uservoicetemp:"", //自己的录音上传后的路径
    status:0,
    scroll_height:800,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    //判断一下从学习记录进的还是从复习进入的
    if (options.source) {
      that.setData({
        source: options.source
      })
    }
    if (that.data.source == 0) {
      that.setData({
        essay_id: options.id,
        has_read: 0,
        scroll_height:windowHeight*750/windowWidth-110-50,
      })

      // 获取文章详情
      that.getEssayDetail();
    } else if (that.data.source == 1) {
      that.setData({
        task_id: options.task_id,
        has_read: 1,
        can_submit: options.can_submit,
        status:options.status
      })
      //获取复习数据
      that.getEssayReview();
    }

    //自然播放结束
    bgMusic.onEnded(() => {
      that.setData({
        starttime: '00:00',
        isOpen: false,
        offset: 0
      })
      console.log("自然播放结束")
    })

    //停止播放
    bgMusic.onStop(() => {
      that.setData({
        starttime: '00:00',
        isOpen: false,
        offset: 0
      })
      console.log("播放停止")
    })
  },
  /**
   * 获取范文详情
   */
  getEssayDetail: function () {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getessaydetail({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      id: that.data.essay_id,
    }).then((res) => {
      //课堂训练图片的url,为空则表示没有，隐藏
      let temurl = res.data.pic;
      let last_writing = 0;
      if (!temurl) {
        last_writing = 0;
      } else {
        last_writing = 1;
      }
      that.setData({
        essay: res.data,
        phrase: res.data.phrase,
        sentence: res.data.sentence,
        src: res.data.essay_url,
        is_class_pic: last_writing,
      })
      WxParse.wxParse('article', 'html', res.data.essay, that, 5)
      WxParse.wxParse('translate', 'html', res.data.translate, that, 5)
      WxParse.wxParse('require', 'html', res.data.require, that, 5)

     
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      isVip: (app.globalData.studentInfo.is_vip == 1 ? true : false)
    })
  },

  //播放
  listenerButtonPlay: function () {
    let that = this;
    bgMusic.title = '全文读音';
    if (bgMusic.src == that.data.src) {

    } else {
      bgMusic.src = that.data.src;
    }

    bgMusic.onTimeUpdate(() => {
      console.log(bgMusic.currentTime)
      var duration = bgMusic.duration;
      var offset = bgMusic.currentTime;
      var currentTime = parseInt(bgMusic.currentTime);
      var min = "0" + parseInt(currentTime / 60);
      var max = parseInt(bgMusic.duration)
      var total_second = parseInt(duration) % 60;
      var sec = currentTime % 60;
      if (total_second < 10) {
        total_second = "0" + total_second;
      }
      if (sec < 10) {
        sec = "0" + sec;
      }
      var starttime = min + ":" + sec;
      var totaltime = "0" + parseInt(duration / 60) + ":" + total_second;
      that.setData({
        duration: totaltime,
        offset: currentTime,
        starttime: starttime,
        max: max,
        changePlay: true
      })
    })

    bgMusic.play();
    that.setData({
      isOpen: true,
    })
  },
  //暂停播放
  listenerButtonPause() {
    let that = this;
    bgMusic.pause();
    that.setData({
      isOpen: false,
    })
  },

  listenerButtonStop() {
    let that = this;
    bgMusic.stop()
    that.setData({
      starttime: '00:00',
      isOpen: false,
      offset: 0
    })
  },

  /**
   *进度条拖拽
   */
  sliderChange(e) {
    console.log("进度条拖拽，e:", e)
    let that = this;
    let offset = parseInt(e.detail.value);
    bgMusic.seek(offset);
    bgMusic.play();
    that.setData({
      isOpen: true,
    })
  },

  /**
   *点击底部题目要求
   */
  showRequirePop: function () {
    let that = this;
    that.setData({
      flag: false, //弹出题目要求框
    })

  },

  /**关闭modal弹窗 */
  closeMask: function () {
    this.setData({
      flag: true
    })
  },

  /**
   * 查看翻译
   */
  lookTranslate: function () {
    let that = this;
    let is_show_translate = this.data.is_show_translate;
    if (is_show_translate == 0) {
      that.setData({
        is_show_translate: 1
      })
    } else if (is_show_translate == 1) {
      that.setData({
        is_show_translate: 0
      })
    }

  },



  // 查看课堂训练的大图
  lookpicutre: function () {
    let that = this;
    //修改本句的imgurl为上次的仿写地址即可
    let imgUrl = that.data.essay.pic;
    //在新界面预览样例大图
    wx.previewImage({
      current: imgUrl,
      urls: [imgUrl],
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {


  },

  /**
   * 生命周期函数--监听页面卸载
   * 页面卸载时停止播放
   */
  onUnload: function () {
    let that = this;
    //停止播放
    that.listenerButtonStop();

    console.log("离开界面")
  },

  /**点击组件弹窗中的开通vip按钮 */
  goOpen: function () {
    wx.navigateTo({
      url: "/pages/vip/vipBuy",
    });
    this.setData({
      showVipModal: false,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})